/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webOffline;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;

/**
 *
 * @author Do Viet Trung
 */
public class Option implements ActionListener {
    public static boolean RIGHT_TO_LEFT = false;
    public static String FileTypePath = "Resources/Database/FileTypes.xml";
    private JFrame frame;
    private JDialog dialog;
    private JButton addBtn,
            removeBtn,
            okBtn,
            cancelBtn;
    private JTextField fileTypeTxt;
    private JList listExtTxt;
    private ArrayList<String> arrExt = new ArrayList<String>();
    private boolean hasChangeTypes = false;
    
    private JPanel createCenterPane() {
        JPanel centerPane = new JPanel();
        JTabbedPane tabbedPane = new JTabbedPane();
        JComponent panel1 = makeComponent();
        tabbedPane.addTab("File types", panel1);
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        JComponent panel2 = makeTextPanel("Panel #2");
        tabbedPane.addTab("Advance",panel2);
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        centerPane.add(tabbedPane);

        return centerPane;
    }
    
    private JPanel createPageEnd() {
        JPanel panePageEnd = new JPanel(new FlowLayout(2));
        okBtn = new JButton("OK");
        okBtn.addActionListener(this);
        okBtn.setActionCommand("okBtn");

        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);;
        cancelBtn.setActionCommand("cancelBtn");

        panePageEnd.add(okBtn);
        panePageEnd.add(cancelBtn);

        return panePageEnd;
    }
    
    protected JComponent makeComponent() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(420,250));
        FlowLayout f = new FlowLayout(0);
        JPanel paneTop = new JPanel(f);

        addBtn = new JButton("Add");
        addBtn.setActionCommand("addBtn");
        addBtn.addActionListener(this);

        removeBtn = new JButton("Remove");
        removeBtn.addActionListener(this);
        removeBtn.addActionListener(this);
        removeBtn.setActionCommand("removeBtn");

        fileTypeTxt = new JTextField(10);

        paneTop.add(new JLabel("Extension:"));
        paneTop.add(fileTypeTxt);
        paneTop.add(addBtn);
        paneTop.add(removeBtn);
        
        try {
            arrExt = DOMXml.readFileTypes(FileTypePath);
            listExtTxt = new JList(arrExt.toArray());
            listExtTxt.setAutoscrolls(true);
            listExtTxt.setOpaque(true);
            listExtTxt.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            listExtTxt.setLayoutOrientation(JList.VERTICAL_WRAP);
            listExtTxt.setVisibleRowCount(13);
            listExtTxt.setBorder(BorderFactory.createLineBorder(Color.lightGray, 1));
            JScrollPane scrollList = new JScrollPane(listExtTxt);
            panel.add(paneTop,BorderLayout.PAGE_START);
            panel.add(scrollList, BorderLayout.CENTER);
        }catch (Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        return panel;
    }

    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);

        return panel;
    }

    private Container addComponentsToPane() {
        Container pane = frame.getContentPane();
        
        if (!(pane.getLayout() instanceof BorderLayout)) {
            pane.add(new JLabel("Container doesn't use BorderLayout!"));
            return null;
        }

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(
                java.awt.ComponentOrientation.RIGHT_TO_LEFT);
        }

        pane.add(createCenterPane(), BorderLayout.CENTER);
        pane.add(createPageEnd(), BorderLayout.PAGE_END);

        return pane;
    }
    
    public void actionPerformed (ActionEvent e) {
        try {
            String cmd = e.getActionCommand();
            
            if(cmd.equals("addBtn")) {
                String txt = fileTypeTxt.getText();
                
                if(txt != null && !txt.equals("")) {
                    if(!arrExt.contains(txt)) {
                        arrExt.add(txt);
                        listExtTxt.setListData(arrExt.toArray());
                        hasChangeTypes = true;
                    }
                }
                fileTypeTxt.setText("");
            }
            else if(cmd.equals("removeBtn")) {
                int i = listExtTxt.getSelectedIndex();
                if (i>=0) {
                    arrExt.remove(i);
                    listExtTxt.setListData(arrExt.toArray());
                    hasChangeTypes = true;
                }
            }
            else if(cmd.equals("okBtn")) {
                if(hasChangeTypes) {
                    DOMXml.createFileTypes(arrExt, FileTypePath);
                    frame.setVisible(false);
                }
            }
            else if(cmd.equals("cancelBtn")) {
                frame.setVisible(false);
            }
        }catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void createAndShowGUI() {
       try {
           UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
       }catch (Exception ex) {
           JOptionPane.showMessageDialog(frame, ex.getMessage());
       }

       //Create and set up the window.
       frame = new JFrame("Option");
       dialog = new JDialog(frame,"Option", true);
       dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
       dialog.setContentPane(addComponentsToPane());
       dialog.setPreferredSize(new Dimension(430, 350));
       dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(WebOffline.imagesPath + "optionIcon.png"));
       dialog.setResizable(false);
       dialog.setLocation(450, 200);
       dialog.pack();
       dialog.setVisible(true);
    }
}
